/**
 * 测试的管理
 * Exam、Mental、Student的关联
 */

Ext.namespace("admin.mental");

admin.mental.test = function(pannelId, title, iconClz){

    var optBar = [{
        text : "导入",
        iconCls : "import",
        handler : importIn
    }];

    var mentalStore = new Ext.data.JsonStore({
        url : "admin/mental/listAll",
        totalProperty : "total",
        root : "data",
        autoLoad : true,
        fields : ["id", "name", "desc"]
    });
    var mentalCm = new Ext.grid.ColumnModel([{
        header : "测试类型",
        dataIndex : "name"
    }]);
    var mentalSm = new Ext.grid.RowSelectionModel({
        singleSelect : true,
        listeners : {
            rowselect : function(){
                var rec = mentalSm.getSelected();
                rubricStore.load();
            }
        }
    });
    var mentalGrid = new Ext.grid.GridPanel({
        title : "测试类型",
        frame : true,
        flex : 3,
        store : mentalStore,
        cm : mentalCm,
        sm : mentalSm
    });

    var rubricStore = new Ext.data.JsonStore({
        url : "admin/rubric/listByMental",
        totalProperty : "total",
        root : "data",
        fields : ["id", "num", "stem", "choice1", "choice2", "choice3", "choice4", "choice5"],
        listeners : {
            beforeload : function(store){
                var rec = mentalSm.getSelected();
                if(rec==undefined){
                    return false;
                }
                store.removeAll();
                Ext.apply(store.baseParams, {
                    mentalId : rec.get("id")
                });
            }
        }
    });
    var rubricCm = new Ext.grid.ColumnModel([{
        header : "序号",
        dataIndex : "num"
    }, {
        header : "题目",
        dataIndex : "stem",
        width : 200
    }, {
        header : "选项1",
        dataIndex : "choice1"
    }, {
        header : "选项2",
        dataIndex : "choice2"
    }, {
        header : "选项3",
        dataIndex : "choice3"
    }, {
        header : "选项4",
        dataIndex : "choice4"
    }, {
        header : "选项5",
        dataIndex : "choice5"
    }]);
    var rubricSm = new Ext.grid.RowSelectionModel({
        singleSelect : true
    });
    var rubricGrid = new Ext.grid.GridPanel({
        title : "测试题目",
        frame : true,
        flex : 7,
        store : rubricStore,
        cm : rubricCm,
        sm : rubricSm
    });

    return new Ext.Panel({
        title : title,
        id : pannelId,
        iconCls : iconClz,
        closable : true,
        layout : "hbox",
        layoutConfig : {
            align : "stretch"
        },
        items : [mentalGrid, rubricGrid],
        tbar : genToolBar(optBar, [], pannelId)
    });

    function importIn() {
        var form = new Ext.form.FormPanel({
            fileUpload : true,
            frame : true,
            labelWidth: 30,
            url : "admin/mental/rubric/importIn",
            defaults : {
                anchor : "100%",
                allowBlank : false
            },
            items : [genMentalCombo(), {
                fieldLabel : "文件",
                emptyText: '请选择文件',
                anchor: '100%',
                xtype : "fileuploadfield",
                name : 'file',
                buttonText : "浏览"
            }],
            buttons : [{
                text : "确定",
                iconCls : "save",
                handler : function(b){
                    submitForm(b.findParentByType("form"), refreshGrid);
                    function refreshGrid(){
                        rubricStore.load();
                    }
                }
            }, {
                text : "重置",
                iconCls : "reset",
                handler : function(b){
                    b.findParentByType("form").getForm().reset();
                }
            }]
        });
        var win = new Ext.Window({
            title : "上传文件",
            width : 300,
            height : 150,
            layout : "fit",
            frame : true,
            items : [form]
        });
        win.show();
    }

    function genMentalCombo(){
        var mentalStore = new Ext.data.JsonStore({
            url : "admin/mental/listAll",
            totalProperty : "total",
            root : "data",
            fields : ["id", "name"]
        });
        var mentalCombo = new Ext.form.ComboBox({
            store : mentalStore,
            fieldLabel : "班级",
            editable : false,
            resizable : true,
            width : 70,
            mode : "remote",
            displayField:"name",
            valueField: "id",
            triggerAction : "all",
            hiddenName : "mentalId"
        });
        return mentalCombo;
    }

}

registerModule("admin.mental.test", admin.mental.test);